<template>
	<view class="mobilephoneLogin">
		<u-navbar back-icon-color="#000" :back-text-style="{color: '#000'}" back-text="手机号登陆" />
		<view class="content">
			<view class="tips">未注册手机号登陆后将自动创建账号</view>
			<view class="inputs">
				<view class="countryCode" @click="openCountryCode">
					{{ countryCode }}
					<view style="display: inline-block;margin: 0 3px;">
						<uni-icons type="arrowdown" color="black" size="12"></uni-icons>
					</view>
				</view>
				<input type="number" placeholder="请输入手机号" v-model="phone" />
				<uni-icons v-show="phone" @click="phone = ''" style="transform: translateY(20%);" type="closeempty" color="#ccc"
				 size="15"></uni-icons>
			</view>
			<commonBtn title="下一步" @click="jumpToNext"></commonBtn>
		</view>
		<u-popup v-model="showCountryCode" mode="bottom">
			<view>出淤泥而不染，濯清涟而不妖</view>
		</u-popup>
		<countryCodePicker ref="countryCode" :transmit="receiveCountryCode" />
	</view>
</template>

<script>
	import {
		checkRegister
	} from '@/api/account.js';
	import {
		sentCaptcha
	} from '@/api/captcha.js';
	export default {
		data() {
			return {
				phone: null,
				countryCode: '+86',
				tel: '86',
				showCountryCode: false
			};
		},
		//只能在页面中监听，不能在自定义组件中监听
		onBackPress(e) {
			if (e.from == 'backbutton' && this.$refs.countryCode.showPicker) {
				this.$refs.countryCode.hide();
				return true; //阻止默认返回
			}
		},
		methods: {
			openCountryCode() {
				// this.$refs.countryCode.show()
				this.$refs.countryCode.open()
			},
			receiveCountryCode(res) {
				this.countryCode = res.countryCode;
				this.tel = res.tel;
			},
			jumpToNext() {
				if (!this.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
				} else if (!this.$phoneReg.test(this.phone)) {
					uni.showToast({
						title: '请输入11位数的手机号',
						icon: 'none'
					});
				} else {
					uni.showLoading({
						mask: true
					});
					checkRegister({
							phone: this.phone
						})
						.then(res => {
							uni.hideLoading();
							if (res.code == 200) {
								//密码登录
								if (res.exist != -1) {
									uni.navigateTo({
										url: '/pages/login/mobilephoneLogin/passwordLogin?phone=' + this.phone + '&countrycode=' + this.tel
									});
									//注册
								} else {
									uni.showLoading();
									sentCaptcha({
											phone: this.phone,
											ctcode: this.tel
										})
										.then(res => {
											uni.hideLoading();
											if (res.code == 200) {
												uni.navigateTo({
													url: '/pages/login/mobilephoneLogin/inputCaptcha?phone=' + this.phone + '&from=register&countryCode=' +
														this.countryCode
												});
											} else {
												uni.showToast({
													title: res.message || res.msg,
													icon: 'none'
												});
											}
										})
										.catch(err => {
											uni.hideLoading();
											uni.showToast({
												title: '发送失败',
												icon: 'none'
											});
										});
								}
							} else {
								uni.showToast({
									title: res.msg || message
								});
							}
						})
						.catch(err => {
							uni.hideLoading();
						});
				}
			},
			handleBack() {
				uni.navigateBack();
			}
		}
	};
</script>

<style lang="scss" scoped>
	.mobilephoneLogin {
		/deep/.uni-navbar--border {
			border: none;
		}

		.content {
			padding: 20px;
			box-sizing: border-box;

			.tips {
				color: #ccc;
				font-size: 12px;
				margin-top: 6px;
			}

			.inputs {
				display: flex;
				align-items: center;
				width: 100%;
				height: 46px;
				border-bottom: 1px #ccc solid;
				padding-right: 2px;
				margin: 11px 0;

				.countryCode {
					line-height: 52rpx;
					font-size: 16px;

					&:active {
						background-color: #ccc;
					}
				}

				>input {
					flex: 1;
					height: 100%;
					font-size: 16px;
				}
			}
		}
	}
</style>
